package RTDGDiscovery;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

import org.apache.log4j.Priority;

import properties.PropertyManager;

import manager.ServiceManager;
import enums.RTDGRequestType;

import host.IHost;
import host.impl.AdminHost;

public class DiscoveryListener extends Thread{
	private IHost hostGroup;
	private static InetAddress groupAddress;
	public MulticastSocket receptionSocket;
	private int byteArraySize;
	
	public IHost getHostGroup() {
		return hostGroup;
	}

	public void setHostGroup(IHost hostGroup) {
		this.hostGroup = hostGroup;
	}

	public int getByteArraySize() {
		return byteArraySize;
	}

	public void setByteArraySize(int byteArraySize) {
		this.byteArraySize = byteArraySize;
	}

	public DiscoveryListener() {
		
	}
	
	public void initListening(){
		ServiceManager.printLog(Priority.INFO_INT, PropertyManager.properties.getProperty("listenerInitialisation"));
		if(groupAddress == null){
			try {
				groupAddress = InetAddress.getByName(hostGroup.getIpAddress());
			} catch (UnknownHostException e) {
				ServiceManager.printLog(Priority.ERROR_INT, PropertyManager.properties.getProperty("listenerInitialisationError"));
				e.printStackTrace();
			}
		}
		try {
			receptionSocket = new MulticastSocket(hostGroup.getPortNumber());
			receptionSocket.joinGroup(groupAddress);
		} catch (IOException e) {
			ServiceManager.printLog(Priority.ERROR_INT, PropertyManager.properties.getProperty("joinningGroupError"));
			e.printStackTrace();
		}
	}
	
	public void startListening(){
		ServiceManager.printLog(Priority.INFO_INT, PropertyManager.properties.getProperty("listenerStart"));
		initListening();
		start();
	}
	
	public boolean isAlreadyRegistred(String ipAddress, int portNumber){
		for (int i = 0; i < ServiceManager.adminHosts.size(); i++) {
			if((ServiceManager.adminHosts.get(i)).getIpAddress().equals(ipAddress)&&(ServiceManager.adminHosts.get(i)).getPortNumber() == portNumber)
				return true;
		}
		return false;
	}
	
	@Override
	public void run() {
		DatagramPacket packet;
		byte[] byteArray = new byte[byteArraySize];
		String message;
		
		packet = new DatagramPacket(byteArray, byteArray.length);
		
		while (true) {
			try {
				ServiceManager.printLog(Priority.INFO_INT, PropertyManager.properties.getProperty("listenerWaiting"));
				receptionSocket.receive(packet);
				message = (new DataInputStream(new ByteArrayInputStream(byteArray))).readUTF();
				System.out.println(PropertyManager.properties.getProperty("listenerReceive")+" "+message);
				
				//prendre l'adresse ip du packet et faire un REGISTER rmi
				ServiceManager.rmiSender.send(RTDGRequestType.REGISTER, packet.getAddress().getHostAddress(),new Integer(message));
				if(!isAlreadyRegistred(packet.getAddress().getHostAddress(),  new Integer(message)))
					ServiceManager.adminHosts.add(new AdminHost( packet.getAddress().getHostAddress(),  new Integer(message)));
				
			} catch (IOException e) {
				ServiceManager.printLog(Priority.ERROR_INT, PropertyManager.properties.getProperty("listenerError"));
				e.printStackTrace();
			}
		}
	}

}
